package com.cipri.battle.core
{
	import com.cipri.battle.config.Settings;
    import com.cipri.battle.events.GameEvent;
    import com.cipri.battle.events.ShapeEvent;
	import com.cipri.battle.ui.Shape;
	import com.cipri.battle.ui.Table;
	import flash.display.Sprite;
    import flash.geom.Point;
	
	import mx.controls.Button;
	import mx.core.UIComponent;

	public class Battleships extends UIComponent
	{
		//PUBLIC VARS
		
		//PRIVATE VARS 
		private var _width:Number;
		private var _height:Number;
		
		private var _table:Table;
		private var _shapes:Array = new Array();
		private var _remainingShapes:Array = new Array();
		
		public function Battleships()
		{
			_width = Settings.WIDTH;
			_height = Settings.HEIGHT;
			
			drawOutline();
			
			_table = new Table();
			_table.x = Settings.TABLE_X;
			_table.y = Settings.TABLE_Y;
			addChild(_table);
			
            initShapes();
		}
        
        public function positionShapesRandomly():void 
        {
            
        }
        
        public function hit():void 
        {
            
        }
        
        private function initShapes():void 
        {
            var shape:Shape;
			
			for (var i:uint = 1; i <= Settings.SHAPES; i++) 
			{
				shape = new Shape(i);
                addChild(shape);
                shape.addEventListener(ShapeEvent.ROTATE, positionShapes);
                shape.addEventListener(ShapeEvent.DROP, onShapeDropped);
				_shapes.push(shape);
			}
            
            _remainingShapes = new Array();
            _remainingShapes = _remainingShapes.concat(_shapes);
            
            positionShapes();
        }
        
        private function positionShapes(event:ShapeEvent = null):void 
        {
            var xPos:Number = Settings.TABLE_CELL_SIZE * Settings.TABLE_SIZE + Settings.TABLE_X + Settings.SHAPES_SPACING;
            
            for (var i:int = 0; i < _remainingShapes.length; i++) 
            {
                _remainingShapes[i].x = xPos;
                if (i != 0) 
                {
                    _remainingShapes[i].y = _remainingShapes[i - 1].y + _remainingShapes[i - 1].height + Settings.SHAPES_SPACING + Settings.TABLE_Y;    
                }
                else 
                {
                    _remainingShapes[i].y = Settings.TABLE_Y;
                }
            }
        }
		
		private function drawOutline():void 
		{
			graphics.lineStyle(Settings.OUTLINE_THICKNESS, Settings.OUTLINE_COLOR, 1);
			graphics.beginFill(Settings.BACKGROUND_COLOR, 1);			
			graphics.drawRoundRect(0, 0, _width, _height, 24);
			graphics.endFill();
		}
        
        private function onShapeDropped(event:ShapeEvent):void 
        {
            var shape:Shape = Shape(event.target);
            if (_table.placeShape(shape)) 
            {
                _remainingShapes.splice(_remainingShapes.indexOf(shape), 1);
                positionShapes();
                //removeChild(shape);
            }
            if (_remainingShapes.length == 0) 
            {
                //no more shapes to place
                _table.startGame();
                _table.addEventListener(GameEvent.GAME_OVER, onGameOver);
                _table.addEventListener(GameEvent.MISSED_HIT, onMissedHit);
                _table.addEventListener(GameEvent.SHAPE_DOWN, onShapeDown);
                _table.addEventListener(GameEvent.SUCCESSFUL_HIT, onSuccessfulHit);
                
                for (var i:int = 0; i < _shapes.length; i++) 
                {
                    removeChild(_shapes[i]);
                }
            }
        }
        
        private function onSuccessfulHit(e:GameEvent):void 
        {
            dispatchEvent(e);
        }
        
        private function onShapeDown(e:GameEvent):void 
        {
            dispatchEvent(e);
        }
        
        private function onMissedHit(e:GameEvent):void 
        {
            dispatchEvent(e);
        }
        
        private function onGameOver(e:GameEvent):void 
        {
            dispatchEvent(e);
        }
		
		//private function  
		
	}
}